import { Snake } from '../entities/Snake';
import { Food } from '../entities/Food';
import { Vector2D } from '../utils/Vector2D';

export type GameMode = 'classic' | 'timeAttack' | 'maze' | 'boss';

export interface GameModeConfig {
    duration?: number;        // 限时模式的持续时间（毫秒）
    obstacles?: Vector2D[];   // 迷宫模式的障碍物位置
    bossInterval?: number;    // BOSS模式的BOSS出现间隔
}

export class GameModeManager {
    private currentMode: GameMode = 'classic';
    private config: GameModeConfig = {};
    private timeLeft: number = 0;
    private bossTimer: number = 0;

    constructor() {}

    public setMode(mode: GameMode, config: GameModeConfig = {}): void {
        this.currentMode = mode;
        this.config = config;

        // 初始化模式特定的状态
        switch (mode) {
            case 'timeAttack':
                this.timeLeft = config.duration || 180000; // 默认3分钟
                break;
            case 'boss':
                this.bossTimer = config.bossInterval || 30000; // 默认30秒
                break;
        }
    }

    public update(deltaTime: number, snake: Snake, foods: Food[]): void {
        switch (this.currentMode) {
            case 'timeAttack':
                this.updateTimeAttack(deltaTime);
                break;
            case 'boss':
                this.updateBossMode(deltaTime, foods);
                break;
        }
    }

    private updateTimeAttack(deltaTime: number): void {
        this.timeLeft -= deltaTime;
        if (this.timeLeft <= 0) {
            // 触发游戏结束
            this.timeLeft = 0;
        }
    }

    private updateBossMode(deltaTime: number, foods: Food[]): void {
        this.bossTimer -= deltaTime;
        if (this.bossTimer <= 0) {
            // 生成BOSS食物
            this.spawnBossFood(foods);
            this.bossTimer = this.config.bossInterval || 30000;
        }
    }

    private spawnBossFood(foods: Food[]): void {
        // 在随机位置生成一个大型食物
        const position = new Vector2D(
            Math.random() * 800,  // 需要根据实际画布大小调整
            Math.random() * 600
        );
        const bossFood = new Food(position, 'boss');
        foods.push(bossFood);
    }

    public getCurrentMode(): GameMode {
        return this.currentMode;
    }

    public getTimeLeft(): number {
        return this.timeLeft;
    }

    public getBossTimer(): number {
        return this.bossTimer;
    }
}